package cn.org.alan.exam.web.controller;

import cn.org.alan.exam.common.result.Result;
import cn.org.alan.exam.model.entity.Job;
import cn.org.alan.exam.model.entity.UserJobDetails;
import cn.org.alan.exam.model.vo.job.UserJobDetailsVo;
import cn.org.alan.exam.service.IJobService;
import cn.org.alan.exam.service.IUserJobDetailsService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 说明：
 * 作业接口
 *
 * @Author Alan
 * @Version 1.0
 * @Date 2025/4/2 6:12 PM
 */
@RestController
@RequestMapping("/api/job")
@Api(tags = "作业管理")
public class JobController {
    @Autowired
    private IJobService jobService;
    @Autowired
    private IUserJobDetailsService userJobDetailsService;

    @PostMapping
    @ApiOperation("添加作业")
    @PreAuthorize("hasAnyAuthority('role_teacher')")
    Result add(@Validated @RequestBody Job job) {
        Job add = jobService.add(job);
        return Result.success("添加作业成功", add);
    }
    @DeleteMapping
    @ApiOperation("删除作业")
    @PreAuthorize("hasAnyAuthority('role_teacher','role_admin')")
    Result delete(Integer jobId) {
        Integer delete = jobService.delete(jobId);
        return Result.success("删除作业成功",delete);
    }
    @PutMapping
    @ApiOperation("修改作业")
    @PreAuthorize("hasAnyAuthority('role_teacher','role_admin')")
    Result update(@Validated @RequestBody Job job) {
        Job resultUpdate = jobService.update(job);
        return Result.success("删除作业成功",resultUpdate);
    }

    @GetMapping("/getTeacherJobPage")
    @ApiOperation("教师获取作业列表")
    @PreAuthorize("hasAnyAuthority('role_teacher','role_admin')")
    Result getTeacherJobPage(@RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                             @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
                             String title,
                             Integer gradeId) {
        Page page = jobService.getTeacherJobPage(pageNum, pageSize, title, gradeId);
        return Result.success("获取作业成功",page);
    }
    @GetMapping("/getJobDetails")
    @ApiOperation("回答用户列表")
    @PreAuthorize("hasAnyAuthority('role_teacher','role_admin')")
    Result getJobDetails(@RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                         @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
                         Integer jobId) {
        Page page = jobService.getJobDetails(pageNum, pageSize, jobId);
        return Result.success("分页获取作业成功",page);
    }
    @GetMapping("/getUserJobDetails")
    @ApiOperation("作业详情")
    @PreAuthorize("hasAnyAuthority('role_teacher','role_admin','role_student')")
    Result getUserJobDetails(Integer jobId,Integer userId) {
        UserJobDetailsVo userJobDetails = jobService.getUserJobDetails(jobId,userId);
        return Result.success("获取用户回答详情成功",userJobDetails);
    }

    @PostMapping("/addUserJobDetails")
    @ApiOperation("用户提交作业")
    @PreAuthorize("hasAnyAuthority('role_student')")
    Result addUserJobDetails(@Validated @RequestBody UserJobDetails userJobDetails) {
        UserJobDetails add = userJobDetailsService.add(userJobDetails);
        return Result.success("用户回答作业成功",add);
    }

    @GetMapping("/getStudentJobPage")
    @ApiOperation("班级下到作业")
    @PreAuthorize("hasAnyAuthority('role_student')")
    Result getStudentJobPage(@RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                             @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
                             String title) {
        Page page = userJobDetailsService.getStudentJobPage(pageNum, pageSize,title);
        return Result.success("获取学生班级下到作业成功",page);
    }

    @PutMapping("/mark")
    @ApiOperation("教师打分")
    @PreAuthorize("hasAnyAuthority('role_teacher')")
    Result mark(Integer jobId,Integer userId,Double score){
        jobService.mark( jobId, userId, score);
        return Result.success("打分成功");
    }

}
